<?php
namespace app\houtai\controller\system;
use app\houtai\controller\BaseController;

class CodeController extends BaseController {
  /**
   *  @brief  : 六位靓号查询
   *  @return : page
   *  @details: 
   */
  public function indexAction() {
    $conds = [];
    $order = [];
    $CHANNELS = $this->GetAdminChannel();
    $ACCOUNTS_TYPE = config('ACCOUNTS_TYPE');
    ////////////////////////////////////////////////////////////////////////////
    $zy = $this->request->param('zy/d', -1);
    $at = $this->request->param('at/d', -1);
    //机器人占用亮号查询
      if ($at == 3){
          $conds['b.RobotID'] = ['egt',1000000];
          $search = $this->request->param('search/s', '');
          if (!empty($search)) {
              $conds['a.Code'] = ['LIKE', "%{$search}%"];
          }
          ////////////////////////////////////////////////////////////////////////////
          $orderMap = [
              'B' => 'GameID',
              'D' => 'UserID'
          ];
          $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'D';
          $order[$orderMap[$this->orderField]] = $this->orderDirection;
          ////////////////////////////////////////////////////////////////////////////
          $list = table('CYWebSMSDB.dbo.CodeIdentifier a', 'rydb.websms')->field([
              'a.Code' => 'GameID',
              'ISNULL(b.RobotID, 0)'         => 'UserID',
              "ISNULL(b.NickName, '')"      => 'NickName',
              "ISNULL(b.HeadImageUrl, '')"    => 'HeadImgUrl',
          ])
              ->join('CYBasicDB.dbo.RobotInfo b', 'a.Code = b.GameID', 'LEFT')
              ->where($conds)->order($order)->paginate();
          ////////////////////////////////////////////////////////////////////////////
          return $this->fetch('', [
              'ACCOUNTS_TYPE' => $ACCOUNTS_TYPE,
              'at'  => $at,
              'zy'  => $zy,
              'search'  => $search,
              'list'  => $list,
          ]);
      }else {
          if ($zy != 0 && $at >= 0 && array_key_exists($at, $ACCOUNTS_TYPE)) {
              $conds['b.AccountsType'] = $at;
          }
          if ($zy >= 0 && in_array($zy, [0, 1])) {
              if ($zy == 0) { // 未占用
                  $conds['b.UserID'] = NULL;
              } else if ($zy == 1) { // 已占用
                  $conds['b.UserID'] = ['GT', 0];
              }
          }
          ////////////////////////////////////////////////////////////////////////////
          $search = $this->request->param('search/s', '');
          $conds['b.Channel'] = [['IN', array_keys($CHANNELS)], NULL, 'OR'];
          if (!empty($search)) {
              $conds['a.Code'] = ['LIKE', "%{$search}%"];
          }
          ////////////////////////////////////////////////////////////////////////////
          $orderMap = [
              'B' => 'GameID',
              'C' => 'UserID',
              'D' => 'AccountsType',
              'E' => 'Channel',
              'F' => 'RegisterDate',
          ];
          $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'D';
          $order[$orderMap[$this->orderField]] = $this->orderDirection;
          ////////////////////////////////////////////////////////////////////////////
          $list = table('CYWebSMSDB.dbo.CodeIdentifier a', 'rydb.websms')->field([
              'a.Code' => 'GameID',
              'ISNULL(b.UserID, 0)' => 'UserID',
              "ISNULL(b.NickName, '')" => 'NickName',
              "ISNULL(b.AccountsType, 0)" => 'AccountsType',
              "ISNULL(b.Channel, 0)" => 'Channel',
              "ISNULL(b.AccountImgUrl, '')" => 'HeadImgUrl',
              "ISNULL(b.RegisterDate, '')" => 'RegisterDate',
          ])
              ->join('CYBasicDB.dbo.AccountsInfo b', 'a.Code = b.GameID', 'LEFT')
              ->where($conds)->order($order)->paginate();
          ////////////////////////////////////////////////////////////////////////////
          return $this->fetch('', [
              'ACCOUNTS_TYPE' => $ACCOUNTS_TYPE,
              'CHANNELS' => $CHANNELS,
              'at' => $at,
              'zy' => $zy,
              'search' => $search,
              'list' => $list,
          ]);

      }
  }




}
